/*
 * This file is part of Formin Media Library
 * 
 */
package formin.mediaLibrary.gui.components;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import formin.mediaLibrary.io.FileIO;
import formin.mediaLibrary.model.SettingsData;


/**
 * 
 * 
 * @author formin
 *
 */
public class DetailsSettingsPanel extends JPanel implements ActionListener{
	
	//==========================================================================
    // Static attributes
    //==========================================================================
	/**
	 * 
	 */
	private static final long serialVersionUID = -7721286959380075828L;
	
	
	private static final String SAVE_CHANGES = "Save Changes";
	private static final String CANCEL_CHANGES = "Cancel Changes";
	
	
	//==========================================================================
    // Attributes
    //==========================================================================
	private SettingsData settingsData;
	
	private JPanel settingsInputPanel;
	
	private JLabel addressLabel;
	private JLabel userNameLabel;
	private JLabel passwordLabel;
	private JLabel dataBaseLabel;
	
	private JTextField addressTextField;
	private JTextField userNameTextField;
	private JTextField passwordTextField;
	private JTextField dataBaseTextField;
	
	
	private JPanel buttonPanel;
	
	private JButton saveChangesButton;
	private JButton cancelChangesButton;
	
    //==========================================================================
    // Constructors
    //==========================================================================
	public DetailsSettingsPanel(){
		settingsData = FileIO.readSettings();
		
		settingsInputPanel = new JPanel();
		settingsInputPanel.setLayout(new GridLayout(4, 2));
		
		addressLabel = new JLabel("Address:");
		passwordLabel = new JLabel("Password:");
		userNameLabel = new JLabel("UserName:");
		dataBaseLabel = new JLabel("DataBase Name:");
		
		addressTextField = new JTextField(settingsData.getSQLServerAddress());
		dataBaseTextField = new JTextField(settingsData.getSQLServerDataBase());
		passwordTextField = new JTextField(settingsData.getSQLServerPassword());
		userNameTextField = new JTextField(settingsData.getSQLServerUserName());
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		saveChangesButton = new JButton(SAVE_CHANGES);
		saveChangesButton.addActionListener(this);
		cancelChangesButton = new JButton(CANCEL_CHANGES);
		cancelChangesButton.addActionListener(this);
		
		settingsInputPanel.add(addressLabel);
		settingsInputPanel.add(addressTextField);
		settingsInputPanel.add(userNameLabel);
		settingsInputPanel.add(userNameTextField);
		settingsInputPanel.add(passwordLabel);
		settingsInputPanel.add(passwordTextField);
		settingsInputPanel.add(dataBaseLabel);
		settingsInputPanel.add(dataBaseTextField);
		
		buttonPanel.add(saveChangesButton);
		buttonPanel.add(cancelChangesButton);
		
		this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		
		add(settingsInputPanel);
		add(buttonPanel);
	}

	

    //==========================================================================
    // Getters
    //==========================================================================

    //==========================================================================
    // Setters
    //==========================================================================

    //==========================================================================
    // Inherited methods
    //==========================================================================
	@Override
	public void actionPerformed(ActionEvent e) {
		
		
		if(e.getActionCommand() == SAVE_CHANGES){
			settingsData.setSQLServerAddress(addressTextField.getText());
			settingsData.setSQLServerDataBase(dataBaseTextField.getText());
			settingsData.setSQLServerPassword(passwordTextField.getText());
			settingsData.setSQLServerUserName(userNameTextField.getText());
			FileIO.writeSettings(settingsData);
			//update changes to settings file
		}else if(e.getActionCommand() == CANCEL_CHANGES){
			settingsData = FileIO.readSettings();
			addressTextField.setText(settingsData.getSQLServerAddress());
			dataBaseTextField.setText(settingsData.getSQLServerDataBase());
			passwordTextField.setText(settingsData.getSQLServerPassword());
			userNameTextField.setText(settingsData.getSQLServerUserName());
			//revert changes to original values
		}
		
	}
	
    //==========================================================================
    // Static methods
    //==========================================================================

}
